package com.example.ytr.startproject_104ytr;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.ytr.startproject_104ytr.bean.WeatherInfo;
import com.example.ytr.startproject_104ytr.iface.WeatherListener;

import com.example.ytr.startproject_104ytr.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements WeatherListener,View.OnClickListener{
    private EditText etcity;
    private Button bthfind1,bthfind2;
    private TextView tvciytname;
    private  TextView cityno;
    private  TextView wd;
    private  TextView ws;
    private  TextView sd;
    private  TextView temp;


    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//调用初始化方法
    }
    private  void init(){
        etcity=(EditText)findViewById(R.id.editText);
        bthfind1=(Button)findViewById(R.id.button);
        bthfind2=(Button)findViewById(R.id.button2);
        tvciytname=(TextView)findViewById(R.id.textView);
        bthfind1.setOnClickListener(this);
        bthfind2.setOnClickListener(this);
        cityno=(TextView)findViewById(R.id.tv_city_no);
        temp=(TextView)findViewById(R.id.tv_temp);
        wd=(TextView)findViewById(R.id.tv_WD);
        ws=(TextView)findViewById(R.id.tv_ws);
        sd=(TextView)findViewById(R.id.tv_SD);
    }

    @Override
    public void onResponse(WeatherInfo weatherInfo) {
        if(weatherInfo!=null){
            tvciytname.setText(weatherInfo.getCity());
            cityno.setText(weatherInfo.getCity());
            temp.setText(weatherInfo.getTemp());
            wd.setText(weatherInfo.getWD());
            ws.setText(weatherInfo.getWS());
            sd.setText(weatherInfo.getSD());
        }
        else  tvciytname.setText("未知");
    }

    @Override
    public void onFail(String msg) {
        tvciytname.setText(msg);

    }
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:

                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(etcity.getText().toString(), this);
                break;
            case R.id.button2:
               WeatherModel weather51Model=new WeatherModel();
                weather51Model.getWeather(etcity.getText().toString(),this);
                break;
            default:
                break;
        }

    }
}
